# -*- coding:utf-8 -*-

import json
import os

from munch import munchify

from Util import config, logger


class Recording(object):

    def __init__(self):
        pass

    # 获取 record.json 文件目录
    @property
    def _getRecordingPath(self):
        return os.path.join(config.testDataPath, 'localMemory', 'record.json')

    # 写入记录文件
    def writeDataToRecord(self, dataDict, dataType='boeData'):
        if not os.path.exists(self._getRecordingPath):
            with open(self._getRecordingPath, 'w', encoding='utf-8') as f:
                json.dump({}, f)
        with open(self._getRecordingPath, "r", encoding="UTF-8") as f:
            rData = json.load(f)
        logger.info(f'{dataType} 的值为：{rData.get(dataType)}')
        if rData.get(dataType) is None:
            rData.update({dataType: {}})
        rData[dataType].update(dataDict)
        data = rData
        with open(self._getRecordingPath, "w", encoding="UTF-8") as f:
            json.dump(data, f, ensure_ascii=False)
        logger.debug('记录文件信息, 文件信息为 -> {} : {}'.format(dataType, dataDict))

    # 读取记录文件
    def readDataFromRecord(self, type='boeData'):
        with open(self._getRecordingPath, "r", encoding="UTF-8") as f:
            rData = munchify(json.load(f))
        return rData.get(type)


record = Recording()

if __name__ == '__main__':
    print(record.writeDataToRecord({'boeNo': '5423452345'}))